If you are a WordPress developer or website owner and have worked on a WordPress blog site where many users upload articles, you may have noticed that even though you cannot edit other user’s posts. Inside the WordPress admin, you can still see the titles of all posts.
How to disable showing of posts and media of other WordPress users
By default, WordPress allows authors to see the titles of every published post, draft, and media.
Also Read: Easy Way to Add Custom Fields into WooCommerce Checkout Page
Yes, you may argue that WordPress already includes some roles (such as Administrator, Editor, and so on). and you want the users to be able to only view and edit their own posts, but you don’t want them to be able to view or edit posts or pages created by other users. you can refer to the below screenshot
In this tutorial, we’ll show you all the steps on how to hide the posts and media of other WordPress users
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Before you start, please create a backup of your WordPress site. In case anything goes wrong while you’re tweaking the code.
Step-1: Hide the posts and media of other WordPress users
Open your theme functions.php file and add the below code to hide the posts and media of other WordPress users.
2 3 4 5 6 7 8 9 10 11 12 |
add_action('pre_get_posts', 'wptw_query_disable_post_media' ); function wptw_query_disable_post_media( $wp_query ) { global $current_user; if( is_admin() && !current_user_can('edit_others_posts') ) { $wp_query->set( 'author', $current_user->ID ); add_filter('views_edit-post', 'wptw_show_post_counts'); add_filter('views_upload', 'wptw_show_media_counts'); } } |
In the WordPress dashboard, the code above will only hide the titles of other user’s posts. As a result, the post counts are still displayed in all posts, drafts, trash, and pending.
To fix the post count in your filter bars as well, we need to follow the next step.
Also Read: Complete Steps to Create Custom Taxonomy in WordPress
Step-2: Fix Posts counts Only Created By Logged In WordPress User
Add the below code snippet to your theme’s functions.php file. Please note that the below code will work only if it is used along with the above-given code.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
function wptw_show_post_counts($views) { global $current_user, $wp_query; unset($views['mine']); $types = array( array( 'status' => NULL ), array( 'status' => 'publish' ), array( 'status' => 'draft' ), array( 'status' => 'pending' ), array( 'status' => 'trash' ) ); foreach( $types as $type ) { $query = array( 'author' => $current_user->ID, 'post_type' => 'post', 'post_status' => $type['status'] ); $result = new WP_Query($query); if( $type['status'] == NULL ): $class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : ''; $views['all'] = sprintf(__('<a href="%s"'. $class .'>All <span class="count">(%d)</span></a>', 'all'), admin_url('edit.php?post_type=post'), $result->found_posts); elseif( $type['status'] == 'publish' ): $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : ''; $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Published <span class="count">(%d)</span></a>', 'publish'), admin_url('edit.php?post_status=publish&post_type=post'), $result->found_posts); elseif( $type['status'] == 'draft' ): $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : ''; $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'), admin_url('edit.php?post_status=draft&post_type=post'), $result->found_posts); elseif( $type['status'] == 'pending' ): $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : ''; $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'), admin_url('edit.php?post_status=pending&post_type=post'), $result->found_posts); elseif( $type['status'] == 'trash' ): $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : ''; $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'), admin_url('edit.php?post_status=trash&post_type=post'), $result->found_posts); endif; } return $views; } |
Using the above code, the post count in your filter bars is resolved but the media counts issue is still pending. So let’s move to the next step to fix the media count issue as well.
Step-2: Fix media counts
Add the below code snippet to your theme’s functions.php file. In order to make this code works, you must also add the first code, which disables the display of other users’ posts and media.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
function wptw_show_media_counts($views) { global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types; $views = array(); $_num_posts = array(); $count = $wpdb->get_results( " SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = $current_user->ID AND post_status != 'trash' GROUP BY post_mime_type ", ARRAY_A ); foreach( $count as $row ) $_num_posts[$row['post_mime_type']] = $row['num_posts']; $_total_posts = array_sum($_num_posts); $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] ); if ( !isset( $total_orphans ) ) $total_orphans = $wpdb->get_var(" SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = $current_user->ID AND post_status != 'trash' AND post_parent < 1 "); $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts)); foreach ( $matches as $type => $reals ) foreach ( $reals as $real ) $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real]; $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : ''; $views['all'] = "<a href='upload.php'$class>" . sprintf( __('All <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>'; foreach ( $post_mime_types as $mime_type => $label ) { $class = ''; if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) ) continue; if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) ) $class = ' class="current"'; if ( !empty( $num_posts[$mime_type] ) ) $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>'; } $views['detached'] = '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Unattached <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>'; return $views; } |
Now after applying this code, you can upgrade the WordPress functionality that allows you to only show the posts and media of the currently logged-in WordPress user.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Wrapping Words
Well, In this article you will get the complete steps about How to hide the posts and media of other WordPress users. You can extend the functionality as per your requirement. I hope you found this tutorial helpful for your project. Keep learning!
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co